<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * VIP管理控制器
 */
class Vip extends AdminBase
{

    /**
     * VIP订单列表
     */
    public function orderList()
    {
        IS_POST && $this->jump($this->logicVipOrder->vipOrderManage($this->param));
        
        $where = $this->logicVipOrder->getWhere($this->param);
        
        $this->assign('list', $this->logicVipOrder->getVipOrderList($where, true, 'vo.id desc', DB_LIST_ROWS));
        $this->assign('stats', $this->logicVipOrder->getVipOrderStats());
        
        return $this->fetch('vip_order_list');
    }
    
    /**
     * VIP订单统计
     */
    public function orderStats()
    {
        $this->assign('stats', $this->logicVipOrder->getVipOrderStats());
        
        return $this->fetch('vip_order_stats');
    }
    
    /**
     * VIP订单详情
     */
    public function orderDetail()
    {
        $info = $this->logicVipOrder->getVipOrderInfo(['vo.id' => $this->param['id']]);
        
        // 关联订单佣金来源
        $commissionList = $this->logicInvite->getOrderCommissionList(['order_id' => intval($this->param['id'] ?? 0)]);
        $this->assign('commission_list', $commissionList);
        
        $this->assign('info', $info);
        
        return $this->fetch('vip_order_detail');
    }

    /**
     * 订单分成奖励（隐藏菜单）
     */
    public function orderCommission()
    {
        $orderId = intval($this->param['id'] ?? 0);
        $list = $this->logicInvite->getOrderCommissionList(['order_id' => $orderId]);
        $this->assign('list', $list);
        return $this->fetch('vip_order_commission');
    }
    
    /**
     * VIP订单状态管理
     */
    public function orderStatus()
    {
        $this->jump($this->logicVipOrder->vipOrderStatus($this->param));
    }
    
    /**
     * VIP订单导出
     */
    public function orderExport()
    {
        $where = $this->logicVipOrder->getWhere($this->param);
        
        $this->logicVipOrder->exportVipOrderList($where);
    }
    
    /**
     * VIP套餐列表
     */
    public function packageList()
    {
        IS_POST && $this->jump($this->logicVip->packageManage($this->param));
        
        $where = $this->logicVip->getPackageWhere($this->param);
        
        $this->assign('list', $this->logicVip->getPackageList($where, true, 'sort asc, id asc', DB_LIST_ROWS));
        
        return $this->fetch('vip_package_list');
    }
    
    /**
     * VIP套餐添加
     */
    public function packageAdd()
    {
        IS_POST && $this->jump($this->logicVip->packageAdd($this->param));
        
        // 为添加操作设置空的info数组，确保模板中的默认值正确显示
        $this->assign('info', []);
        
        return $this->fetch('vip_package_edit');
    }
    
    /**
     * VIP套餐编辑
     */
    public function packageEdit()
    {
        IS_POST && $this->jump($this->logicVip->packageEdit($this->param));
        
        $info = $this->logicVip->getPackageInfo(['id' => $this->param['id']]);
        
        $this->assign('info', $info);
        
        return $this->fetch('vip_package_edit');
    }
    
    /**
     * VIP套餐删除
     */
    public function packageDelete()
    {
        $this->jump($this->logicVip->packageDel($this->param));
    }
    
    /**
     * 激活VIP订单
     */
    public function orderActivate()
    {
        $this->jump($this->logicVip->vipActivate($this->param));
    }
    
    /**
     * 取消VIP订单
     */
    public function orderCancel()
    {
        $this->jump($this->logicVip->vipCancel($this->param));
    }
    
    /**
     * 退款VIP订单
     */
    public function orderRefund()
    {
        $this->jump($this->logicVip->vipRefund($this->param));
    }
}
